package com.woniuxy.provider;

import com.woniuxy.entity.Dish;
import com.woniuxy.entity.DishCategory;
import org.apache.ibatis.jdbc.SQL;

import java.math.BigDecimal;

public class DishProvider {
    public String select(Dish dish){
        return new SQL(){{
            SELECT("*");
            FROM("gfr_dish");
            if(dish != null){
                if(dish.getDishCategoryId()!=0){
                    WHERE("dishCategoryId='"+dish.getDishCategoryId()+"'");
                }if(dish.getShopId()!=0){
                    WHERE("shopId='"+dish.getShopId()+"'");
                }
            }
        }}.toString();
    }

    public String update(Dish dish) {
        return new SQL() {
            {
                /*双大括号初始化*/
                UPDATE("gfr_dish");
                if (dish.getDishName() != null &&
                        dish.getDishName().length()!= 0) {
                    SET("name='" + dish.getDishName() + "'");
                }
                if (dish.getPicture() != null &&
                        dish.getPicture().length()!= 0) {
                    SET("picture='" + dish.getPicture() + "'");
                }
                BigDecimal number=new BigDecimal(0);
                if (dish.getPrice().compareTo(number) >-1) {
                    SET("price='" + dish.getPrice() + "'");
                }
                if (dish.getStock()>0) {
                    SET("stock='" + dish.getStock() + "'");
                }
                if (dish.getIsNew()!= null && dish.getIsHot().length() != 0) {
                    SET("is_new='" + dish.getIsNew() + "'");
                }
                if (dish.getIsHot()!= null && dish.getIsHot().length() != 0) {
                    SET("is_hot='" + dish.getIsHot() + "'");
                }
                WHERE("id=" + dish.getId());
            }
        }.toString();
    }
}
